package com.meida.student.app.user.handler;

import com.meida.common.base.entity.EntityMap;
import com.meida.common.base.utils.FlymeUtils;
import com.meida.common.mybatis.interceptor.SaveInterceptor;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.mybatis.query.CriteriaSave;
import com.meida.common.security.OpenHelper;
import com.meida.common.utils.RedisUtils;
import com.meida.module.bus.client.entity.BusAfterservice;
import com.meida.module.bus.client.entity.BusOrder;
import com.meida.module.bus.client.entity.BusOrderAnswer;
import com.meida.module.bus.client.enums.OrderTypeEnum;
import com.meida.module.bus.client.enums.RedisKeyEnum;
import com.meida.module.bus.provider.service.BusOrderAnswerService;
import com.meida.module.bus.provider.service.BusOrderService;
import com.meida.msg.client.entity.MsgReceive;
import com.meida.msg.provider.service.MsgReceiveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 申请售后
 *
 * @author zyf
 */
@Component
public class AfterServiceSaveHandler implements SaveInterceptor<BusAfterservice> {

    @Autowired
    private BusOrderService busOrderService;

    @Autowired
    private BusOrderAnswerService busOrderAnswerService;

    @Autowired
    private MsgReceiveService msgReceiveService;

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public ResultBody validate(CriteriaSave cs, EntityMap params) {
        BusAfterservice busAfterservice = cs.getEntity(BusAfterservice.class);
        Long orderId = busAfterservice.getOrderId();
        if (FlymeUtils.isEmpty(orderId)) {
            return ResultBody.failed("缺少订单参数");
        }
        BusOrder busOrder = busOrderService.getById(orderId);
        Integer orderType = busOrder.getOrderType();
        if (FlymeUtils.isEmpty(busOrder)) {
            return ResultBody.failed("该订单不存在");
        }
        BusOrderAnswer orderAnswer = busOrderAnswerService.getAnswerByOrderId(orderId);
        if (FlymeUtils.isEmpty(orderAnswer)) {
            return ResultBody.failed("该订单答案信息不存在");
        }
        Integer isSettle = FlymeUtils.getInteger(orderAnswer.getIsSettle(), 0);
        if (isSettle.equals(1)) {
            return ResultBody.failed("该订单不能申请售后,已超过售后时间");
        }
        Long reasonId = busAfterservice.getReasonId();
        if (FlymeUtils.isEmpty(reasonId)) {
            return ResultBody.failed("请选择原因");
        }
        String afterDescribe = busAfterservice.getAfterDescribe();
        if (FlymeUtils.isEmpty(afterDescribe)) {
            return ResultBody.failed("请填写申请售后服务的具体原因");
        }
        Long answerId = orderAnswer.getAnswerId();
        Long userId = busOrder.getUserId();
        params.put("answerId", answerId);
        params.put("orderType", orderType);
        params.put("orderUserId", userId);
        //todo redis 是否有答案结算的Id,直接移除
        String redisKey = RedisKeyEnum.OrderAnswerId.getRedisKey() + answerId;
        boolean hasKey = redisUtils.hasKey(redisKey);
        if (hasKey) {
            redisUtils.del(redisKey);
        }
        return ResultBody.ok();
    }

    @Override
    public void prepare(CriteriaSave criteriaSave, EntityMap entityMap, BusAfterservice busAfterservice) {
        Long userId = OpenHelper.getUserId();
        busAfterservice.setUserId(userId);
        busAfterservice.setState(0);
        Long answerId = entityMap.getLong("answerId");
        busAfterservice.setAnswerId(answerId);
    }

    @Override
    public void complete(CriteriaSave cs, EntityMap params, BusAfterservice entity) {
        Long orderId = entity.getOrderId();
        //更新订单为售后处理中，3个类型订单状态一致
        busOrderService.updateOrderState(orderId, "6");
        Integer orderType = params.getInt("orderType");
        Long orderUserId = params.getLong("orderUserId");
        //处理订单节点通知-->用户
        sendUserMessage(orderType, orderUserId);
    }

    private void sendUserMessage(Integer orderType, Long orderUserId) {
        OrderTypeEnum orderTypeEnum = OrderTypeEnum.getByValue(orderType);
        String orderTypeDesc = orderTypeEnum.getLabel();
        String msgTitle = orderTypeDesc + "，申请售后通知";
        String msgContent = "您申请的" + orderTypeDesc + "售后，平台已收到，客服人员进行审核中！感谢您使用留同学APP";
        MsgReceive msgReceive = msgReceiveService.sendMsgReceiveNotice(BusOrder.class.getSimpleName(), orderUserId, msgTitle, msgContent);
        busOrderService.orderPushMessage(msgReceive, msgTitle, msgContent);
    }
}
